package nio.bytebuffer;


import java.nio.ByteBuffer;
import java.nio.charset.Charset;

import static nio.bytebuffer.ByteBufferUtil.debugAll;


/**
 * @author ZH
 */
public class TestBufferString {
    public static void main(String[] args) {
        //1.将字符串转为byteBuffer
        byte[] bytes = "hello".getBytes();
        ByteBuffer buffer = ByteBuffer.allocate(16);
        buffer.put(bytes);
        debugAll(buffer);
        //2.Charset
        ByteBuffer buffer2 = Charset.forName("UTF8").encode("hello");
        debugAll(buffer2);

        //3. wrap
        ByteBuffer buffer3 = ByteBuffer.wrap("hello".getBytes());
        debugAll(buffer3);

        //转成字符串
        System.out.println(Charset.forName("UTF8").decode(buffer3));
        System.out.println(Charset.forName("UTF8").decode(buffer2));
        buffer.flip();
        System.out.println(Charset.forName("UTF8").decode(buffer));
    }
}
